package com.punch.user.controller;

import com.punch.common.dto.user.user.PasswordDTO;
import com.punch.common.dto.user.user.UserDTO;
import com.punch.common.dto.user.user.UserOrganAuditDTO;
import com.punch.common.model.LoginUser;
import com.punch.common.model.Result;
import com.punch.common.query.user.user.UserQueryInfo;
import com.punch.common.simple.redis.utils.UserUtil;
import com.punch.common.vo.user.menu.UserMenuTreeVO;
import com.punch.common.vo.user.user.SimpleSchoolVO;
import com.punch.common.vo.user.user.UserExtendVO;
import com.punch.common.vo.user.user.UserVO;
import com.punch.user.service.UserOrganService;
import com.punch.user.service.UserSchoolService;
import com.punch.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * @author 作者 owen E-mail: 624191343@qq.com
 * 用户
 */
@RestController
@Api(tags = "用户模块api")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserOrganService userOrganService;

    @Resource
    private UserSchoolService userSchoolService;

    /**
     * 新增账号
     *
     * @param dto
     * @return
     */
    @PostMapping("/user")
    @ApiOperation(value = "新增账号信息")
    public Result saveUser(@Valid @RequestBody UserDTO dto) {
        return Result.succeed(userService.saveUser(dto));
    }

    /**
     * 修改账号
     *
     * @param userDTO
     * @return
     */
    @PutMapping("/user")
    @ApiOperation(value = "修改账号信息")
    public Result updateUser(@RequestBody UserDTO userDTO) {
        userService.updateUser(userDTO);
        return Result.succeed("修改成功");
    }


    /**
     * 查看用戶详情
     *
     * @param userId 用户uid
     * @param type   0-管理员 1-其他角色
     * @return userVO
     * @author wujj
     * @since 2020/9/14 16:06
     */
    @GetMapping("/user/{userId}")
    @ApiOperation(value = "查看用戶详情", response = UserVO.class)
    public Result<UserVO> getUser(@PathVariable Long userId, @RequestParam(name = "type", defaultValue = "0") Integer type) {
        return Result.succeed(userService.getUser(userId, type));
    }


    /**
     * 用户分页列表
     */
    @GetMapping("/users")
    @ApiOperation(value = "用户分页列表", response = UserVO.class)
    public Result listUser(@ModelAttribute UserQueryInfo queryInfo) {
        return Result.succeed(userService.queryPage(queryInfo));
    }

    /**
     * 机构用户分页列表
     */
    @GetMapping("/user/organs")
    @ApiOperation(value = "机构用户分页列表", response = UserVO.class)
    public Result listOrganUser(@ModelAttribute UserQueryInfo queryInfo) {
        return Result.succeed(userService.listOrganUser(queryInfo));
    }

    /**
     * 学校账号列表
     */
    @GetMapping("/user/schools")
    @ApiOperation(value = "学校账号列表", response = UserVO.class)
    public Result listSchoolUser(@ModelAttribute UserQueryInfo queryInfo) {
        return Result.succeed(userService.listSchoolUser(queryInfo));
    }

    /**
     * 修改机构审核状态
     */
    @PutMapping("/user/organ/audit")
    @ApiOperation(value = "修改机构审核状态")
    public Result updateStatus(@Valid @RequestBody UserOrganAuditDTO dto) {
        userService.auditOrgUser(dto);
        return Result.succeed("修改成功");
    }

    /**
     * 通过手机号查询用户、角色信息
     *
     * @param mobile 手机号
     */
    @GetMapping(value = "/user-anon/mobile", params = "mobile")
    @ApiOperation(value = "根据手机号查询用户")
    public LoginUser getUserByMobile(String mobile) {
        return userService.getUserByMobile(mobile);
    }


    /**
     * 获取当前用户菜单
     */
    @GetMapping(value = "/user/menu-tree")
    @ApiOperation(value = "获取当前用户菜单")
    public Result<List<UserMenuTreeVO>> getUserMenuTree() {
        return Result.succeed(userService.getUserMenuTree());
    }

    /**
     * 修改用户密码
     */
    @PostMapping(value = "/user/password/reset")
    @ApiOperation(value = "修改用户密码")
    public Result<String> resetPassword(@RequestBody PasswordDTO passwordDTO) {
        Long userId = UserUtil.getLoginUser().getUserId();
        return Result.succeed(userService.resetPassword(userId, passwordDTO));
    }


    /**
     * 用户启用/停用
     */
    @PutMapping("/user/enable/{userId}")
    @ApiOperation(value = "用户启用/停用")
    public Result enableUser(@PathVariable Long userId) {
        userService.enableUser(userId);
        return Result.succeed("修改成功");
    }

    /**
     * 学校用户分页列表
     */
    @GetMapping("/user/school/simple")
    @ApiOperation(value = "学校用户分页列表", response = SimpleSchoolVO.class)
    public Result listUserSchool() {
        return Result.succeed(userService.listUserSchool());
    }


    /**
     * 获取机构审批失败信息
     *
     * @param userId 用户id/机构id
     * @return string
     */
    @GetMapping("/user/organ/{userId}/audit-message")
    @ApiOperation(value = "获取机构审批失败信息", response = SimpleSchoolVO.class)
    public Result getOrganAuditMessage(@PathVariable Long userId) {
        return Result.succeed(userService.getOrganAuditMessage(userId), "");
    }

    /**
     * 学校下机构数量和学校开课数量统计
     * <p>
     * 学校下机构数量
     *
     * @return list
     */
    @GetMapping(value = "/user/organ/report", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "学校下机构数量", notes = "学校下机构数量", produces = "application/json")
    public Result organReport() {
        return Result.succeed(userService.organReport());
    }

    /**
     * 学校下开课数量
     *
     * @return list
     */
    @GetMapping(value = "/user/course/report", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "学校下开课数量", notes = "学校下开课数量", produces = "application/json")
    public Result courseReport() {
        return Result.succeed(userService.courseReport());
    }


    /**
     * 重置密码
     *
     * @param userId 用户id
     * @return long
     */
    @PutMapping(value = "/user/pwd/{userId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "重置密码", notes = "重置密码", produces = "application/json")
    public Result<Long> resetPwd(@PathVariable Long userId) {
        return Result.succeed(userService.resetPwd(userId));
    }

    /**
     * 获取用户结算信息
     *
     * @param userId 用户id
     * @return long
     */
    @GetMapping(value = "/user/extend/{userId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "获取用户结算信息", notes = "获取用户结算信息", produces = "application/json")
    public Result<UserExtendVO> getUserSettlement(@PathVariable Long userId, @RequestParam("type") Integer type) {
        //学校
        if (type == 0) {
            return Result.succeed(userSchoolService.getUserSettlement(userId));
        }
        //机构
        if (type == 1) {
            return Result.succeed(userOrganService.getUserSettlement(userId));
        }
        return Result.failed("类型错误", 9999);
    }

    @GetMapping(value = "/user/handlerUserArea", produces = "application/json; charset=utf-8")
    public void handlerUserArea() {
        userService.handlerUserArea();
    }
}
